package com.zscat.mallplus.util;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.zscat.mallplus.utils.CommonResult;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;

import java.io.UnsupportedEncodingException;

import org.apache.commons.codec.binary.Base64;

import com.tencentcloudapi.cms.v20190321.CmsClient;
import com.tencentcloudapi.cms.v20190321.models.*;

/**
 * 微信内容安全识别
 * @author Wsen
 *
 */
public class WxContentUtils {

	private static String secretId = "AKIDJQL4ZIJJLA1dybvesxq30g8l8E5hENSX";
	private static String secretKey = "EF3h6QYSrOra5TzNYWfzqS1UkpBSUHtI";
	
	public static Object Wxcontent(String str) {
		try{
			Base64 base64 = new Base64();
			String base64Sign = base64.encodeToString(str.getBytes("UTF-8"));

            Credential cred = new Credential(secretId, secretKey);

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("cms.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            CmsClient client = new CmsClient(cred, "ap-beijing", clientProfile);

            TextModerationRequest req = new TextModerationRequest();
            req.setContent(base64Sign);

            TextModerationResponse resp = client.TextModeration(req);

            System.out.println(TextModerationResponse.toJsonString(resp));
    		if (resp.getData().getEvilFlag() == 1) {
    			return new CommonResult().failed("回复内容存在非法字段，请重新输入。");
    		}
        } catch (TencentCloudSDKException | UnsupportedEncodingException e) {
                System.out.println(e.toString());
        }
		return null;
	}
}
